//
//  MJBaseNavigationController.swift
//  新浪微博
//
//  Created by MaJi on 16/4/8.
//  Copyright © 2016年 MaJi. All rights reserved.
//

import UIKit

//第二个子视图控制器的返回的按钮的title是第一个控制器的title 以后所有的子视图控制器的title都是返回
//返回手势的解决
class MJBaseNavigationController: UINavigationController,UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        self.interactivePopGestureRecognizer?.delegate = self
        //设置全局颜色
        //全局颜色的设置 必须在控件实例化之前
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
    }
    
    
    //重写了不super
    //根视图控制器无法添加   -> UINavigationController(rootViewController:XXX)
    override func pushViewController(viewController: UIViewController, animated: Bool) {
        super.pushViewController(viewController, animated: animated)
//        print(childViewControllers.count)
        if childViewControllers.count > 1 {
            var titleName:String = "返回"
            if childViewControllers.count == 2 {
//                print(childViewControllers.first?.title)
                titleName = childViewControllers.first?.title ?? ""
            }
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: titleName, imageName: "navigationbar_back_withtext", target: self, action: "back")
//            隐藏底部tab
            viewController.hidesBottomBarWhenPushed = true
            //设置颜色  页面跳转的时候会有颜色切换 导致卡顿(注意一定要加在第一个视图,不能是第0个)
            viewController.view.backgroundColor = UIColor.whiteColor()
        }
        
    }
    
    @objc private func back(){
        self.popViewControllerAnimated(true)
    }
    //手势将要开始的协议方法
    //协议方法返回值是 bool值 如果返回ture  就可以正常工作,如果返回false 手势就会失效
    func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
        return childViewControllers.count != 1
        
//        if childViewControllers.count == 1{
//            return false
//        }else{
//            return true
//        }
    }
}
